<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2025-03-27 14:46:00
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2025-04-03 09:39:23
 * @FilePath: \leeui\lee_ui_test\src\views\El.vue
 * Copyright (c) 2025 by ${git_name_email}, All Rights Reserved. 
-->
<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElForm, ElFormItem, ElInput, ElButton } from "element-plus";

// 表单数据
const formData = reactive({
  name: "",
  password: "",
});

// 使用 ref 获取表单实例
const formRef = ref<InstanceType<typeof ElForm>>();

// 提交表单
const onSubmit = () => {
  // 调用表单验证
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      console.log("表单验证成功", formData);
    } else {
      console.log("表单验证失败");
      return;
    }
  });
};

// 表单验证规则
const rules = {
  name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};
</script>

<template>
  <el-form :model="formData" :rules="rules" ref="formRef" status-icon>
    <el-form-item label="用户名" prop="name">
      <el-input v-model="formData.name" placeholder="请输入用户名" />
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="formData.password"
        placeholder="请输入密码"
        type="password"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">登录</el-button>
    </el-form-item>
  </el-form>
</template>
